package com.digital.herbarium.Controller;


import cn.hutool.core.util.StrUtil;
import com.digital.herbarium.annotation.CheckToken;
import com.digital.herbarium.Entity.ResponseResult;
import com.digital.herbarium.Entity.Specimen;
import com.digital.herbarium.Util.Watermark;
import com.digital.herbarium.annotation.Log;
import com.digital.herbarium.service.SpecimenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

@RestController
@RequestMapping("/specimen")
public class SpecimenController {

    @Autowired
    private SpecimenService specimenService;
    @Value("${static.addressurl}")
    private String addressurl;
    @Value("${static.url}")
    private String staticurl;
    @Value("${static.excelurl}")
    private String excelurl;
    @Value("${watermarkText}")
    private String watermarkText;


    @GetMapping("getSpecimenInfo")
    @Log("specimen")
    public ResponseResult getSpecimenInfo(Specimen specimen,
                                          @RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize,
                                          @RequestParam(value = "pageNum",defaultValue = "0",required = false) int pageNum){
        return ResponseResult.SUCCESS().setData( specimenService.getSpecimenInfo(specimen, pageSize,  pageNum));
    }

    @GetMapping("getManagerSpecimenInfo")
    @CheckToken()
    @Log("specimen")
    public ResponseResult getManagerSpecimenInfo(Specimen specimen,
                                          @RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize,
                                          @RequestParam(value = "pageNum",defaultValue = "0",required = false) int pageNum){
        return ResponseResult.SUCCESS().setData( specimenService.getSpecimenInfo(specimen, pageSize,  pageNum));
    }


    @PostMapping("addSpecimen")
    @CheckToken()
    public  ResponseResult addSpecimen(Specimen specimen){
        Integer id = specimenService.addSpecimen(specimen);
        HashMap<String,Object> map = new HashMap<>();
        map.put("id",id);
        return ResponseResult.SUCCESS().setData(map);
    }
    @PostMapping("upload")
    @CheckToken()
    public  ResponseResult upload(@RequestParam(value = "id",defaultValue = "0",required = false)  Integer id, @RequestParam(value = "file") MultipartFile file, HttpServletRequest request){
        specimenService.addPic(file,request,id,staticurl);
        return ResponseResult.SUCCESS().setData("上传成功");
    }

    @PostMapping("uploadExcel")
    @CheckToken()
    public  ResponseResult uploadExcel( @RequestParam(value = "file") MultipartFile file, HttpServletRequest request){
        specimenService.addExcel(file,request,excelurl);
        return ResponseResult.SUCCESS().setData("上传成功");
    }

    @PostMapping("editSpecimen")
    @CheckToken()
    public ResponseResult editSpecimen(Specimen specimen){
        specimenService.editSpecimen(specimen);
        return  ResponseResult.SUCCESS().setData("更新成功");
    }

    @GetMapping("delSpecimen")
    @CheckToken()
    public ResponseResult delSpecimen(String id){
        specimenService.delSpecimen(id);
        return  ResponseResult.SUCCESS().setData("删除成功");
    }

    @GetMapping("download")
    public ResponseResult downloadImage(HttpServletResponse response,String imageName,String flag) throws IOException, IOException {
        if(StrUtil.hasEmpty(flag) || StrUtil.hasBlank(flag)){
            return  ResponseResult.SUCCESS().setData("操作成功");
        }
        String[] ins = imageName.split(",");
        System.out.println(ins.length);
        for (int i = 0; i < ins.length; i++) {
            System.out.println(ins[i]);
            String url = staticurl + File.separator + ins[i];
            System.out.println(url);
            Watermark.addWaterMark(url, "湖北省药检院数字标本馆");
        }

        return  ResponseResult.SUCCESS().setData("操作成功");
    }
}
